package com.sinosoft.msf.base.supplier.pojo;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import java.util.HashMap;
import java.util.Map;

/**
 * REST返回数据结果
 */
public class RestEntity {
    private static final int CODE_OK = 0;


    private int code;    //异常编码
    private String msg;    //消息
    private Map error;
    private Object data;            //传输数据

    public RestEntity() {
        this.code = CODE_OK;
        this.msg = "操作成功!";
    }

    public RestEntity(Object t) {
        this.code = CODE_OK;
        this.msg = "操作成功!";
        this.data = t;
    }

    public RestEntity(Object t, String msg) {
        this.code = CODE_OK;
        this.msg = msg;
        this.data = t;
    }

    public RestEntity(int errorc, String msg) {
        this.code = errorc;
        this.msg = msg;
    }

    public RestEntity(int errorc, String msg, Map error, Object t) {
        this.code = errorc;
        this.msg = msg;
        this.error = error;
        this.data = t;
    }

    public RestEntity(int errorc, String msg, Object t) {
        this.code = errorc;
        this.msg = msg;
        this.data = t;
    }

    public static RestEntity data(Object t) {
        return new RestEntity(t);
    }

    public static RestEntity data(Object t, String msg) {
        return new RestEntity(t, msg);
    }

    public static RestEntity success() {
        return new RestEntity();
    }

    public static RestEntity success(String msg) {
        return new RestEntity(CODE_OK, msg);
    }

    public static RestEntity error(String errorinfor) {
        return new RestEntity(99, errorinfor);
    }

    public static RestEntity error(int code, String errorinfor) {
        return new RestEntity(code, errorinfor);
    }

    public static RestEntity error(String errorinfor, Object t) {
        return new RestEntity(99, errorinfor, t);
    }

    public static RestEntity error() {
        return new RestEntity(99, "操作失败");
    }

    public static RestEntity error(BaseError errorinfo){
        HashMap<String, Object> error = new HashMap<>();
        error.put("no",errorinfo.getCode1());
        error.put("tip",errorinfo.getErrorMsg());
        error.put("source",null);
        return new RestEntity(99, errorinfo.getErrorMsg(),error,null);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Map getError() {
        return error;
    }

    public void setError(Map error) {
        this.error = error;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
